import statistics.*;
import java.util.Random;

public class MM1Simulation {

  protected double lambda;
  protected double mu;

  public MM1Simulation(double lambda, double mu) {
    this.lambda = lambda;
    this.mu = mu;
  }
  
  public void simulate(double simLength, long seed) {
    Queue q = new Queue();
    FES fes = new FES();

    Random rand = new Random(seed);
    Distribution interarrivalTimeDist = new ExponentialDistribution(lambda, rand);
    Distribution serviceTimeDist = new ExponentialDistribution(mu, rand);
    double t = 0;

    long beginTime = System.currentTimeMillis();
    // generate first arrival
    fes.addEvent(new Event(Event.ARRIVAL, interarrivalTimeDist.nextRandom()));
    
    while (t < simLength) {
      Event e = fes.nextEvent();
      t = e.getTime();
      switch (e.getType()) {
        case Event.ARRIVAL : {
          // schedule next arrival
          fes.addEvent(new Event(Event.ARRIVAL, t + interarrivalTimeDist.nextRandom()));

          double serviceTime = serviceTimeDist.nextRandom();
          q.addCustomer(new Customer(t, serviceTime));
          if (q.getSize() == 1) {
            fes.addEvent(new Event(Event.DEPARTURE, t + serviceTime));
          }
          break;
        }
        case Event.DEPARTURE : {
          q.removeCustomer(t);
          if (q.getSize() > 0) {
            double serviceTime = q.getCustomerAt(0).getServiceTime();
            fes.addEvent(new Event(Event.DEPARTURE, t + serviceTime));
          }
          break;
        }      
      }
    }
	// System.out.println("Calculation time: "+(System.currentTimeMillis() - beginTime)*0.001+" seconds");  
	//System.out.println("LAMDBA: "+lambda+" MU: "+mu);
		//System.out.println("Mean waiting time: "+q.getMeanWaitingTime());
	System.out.println(1000*q.getMeanWaitingTime());
   // System.out.println("Var. waiting time: "+q.getVarianceWaitingTime());
    //System.out.println("Mean response time: "+q.getMeanSojournTime());
    //System.out.println("Var. response time: "+q.getVarianceSojournTime());
    //System.out.println("Mean nr. of. customers: "+q.getMeanNumberOfCustomers());

  }
  
  public static void main(String[] arg) {
  	
	double lmda =50;
	double rho=0.0;
	for (int i = 1; i<10 ; i=i+1){
	rho=0.1*i;
    MM1Simulation s = new MM1Simulation(lmda, lmda/rho);
 	s.simulate(1379,123);
  }
} 
}